﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageNews_ManageNews : System.Web.UI.Page
{
    DAL.NewsClassTableAdapters.NewsClassTableAdapter DNC =
        new DAL.NewsClassTableAdapters.NewsClassTableAdapter();

    MODEL.NewsClass MNC = new MODEL.NewsClass();
    MODEL.NewsClass _MNC = new MODEL.NewsClass();

    MODEL.News MN = new MODEL.News();
    MODEL.News _MN = new MODEL.News();

    BLL.NewsClass BNC = new BLL.NewsClass();
    BLL.News BN = new BLL.News();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        LB_ManageNews.Text = "";

        GV_NewsList.AllowPaging = true;

        if (!IsPostBack)
        {
            DDL_ClassDataBind();

            SessionInit();

            PageDataInit();
        }
    }


    #region 页面Session [初始化]
    protected void SessionInit()
    {
        try
        {
            if (Request.QueryString["ClassID"] == null)
            {
                Session.Remove("NewsClassCode");
            }
            else
            {
                MNC.NC_ID = Convert.ToInt32(Request.QueryString["ClassID"]);

                _MNC = BNC.GetDataByID(MNC);

                Session["NewsClassCode"] = _MNC.NC_ClassCode;
            }

            
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    protected void PageDataInit()
    {
        try
        {
            if (Session["NewsClassCode"] != null)
            {
                GV_NewsList.DataSourceID = "ODS_NewsList2";
                GV_NewsList.DataBind();
            }
            else
            {
                GV_NewsList.DataSourceID = "ODS_NewsList";
                GV_NewsList.DataBind();
            }
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);

            DataTable DT = DNC.GetAllDataOrderByClassCode();

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    FormatClassTitle(dr["NC_Title"], dr["NC_Level"]), dr["NC_ID"].ToString());
                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 是否启用 [格式化]
    protected string FormatIsLock(object _IsLock)
    {
        bool BL = Convert.ToBoolean(_IsLock);

        if (BL)
        {
            return "已锁定";
        }
        else
        {
            return "已启用";
        }
    }
    #endregion


    #region 是否启用 [Command] [GridView]
    protected void Btn_IsLock_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MN.News_ID = Convert.ToInt32(e.CommandArgument);

            BN.ModIsLock(MN);

            GV_NewsList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 修改文章 [Command] [GridView]
    protected void Btn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            Response.Redirect("AddNews.aspx?NewsID="+e.CommandArgument.ToString());
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 删除文章 [Command] [GridView] (标记已删除)
    protected void Btn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MN.News_ID = Convert.ToInt32(e.CommandArgument);

            BN.MarkIsDel(MN);

            GV_NewsList.DataBind();

            LB_ManageNews.Text = "删除成功";
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 添加文章 [Click]
    protected void Btn_AddNews_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("AddNews.aspx?ClassID=" + DDL_Class.SelectedValue);
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion


    #region 文章搜索 [Click]
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            GV_NewsList.AllowPaging = false;

            GV_NewsList.DataSourceID = "ODS_Search";

            GV_NewsList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageNews.Text = ex.Message;
        }
    }
    #endregion
}
